<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.lang.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.security.SecureRandom" %>
<%!
public String generateRandomPassword() {
Random RANDOM = new SecureRandom();
int defaultPasswordLength = 8;
String validLetters = "abcdefghjkmnpqrstuvwxyz" + "ABCDEFGHJKMNPQRSTUVWXYZ" + "23456789";

String newPassword = "";
for (int i = 0; i < defaultPasswordLength; i++) {
int index = (int)(RANDOM.nextDouble() * validLetters.length());
newPassword += validLetters.substring(index, index+1);

}

return newPassword;

}
%>
<%
try {
String connectionURL = "jdbc:mysql://webproject2.cse.ust.hk/BookwarmMobile?user=BookwarmMobile&password=bookwarm"; 

Connection connection = null; 

Class.forName("com.mysql.jdbc.Driver").newInstance(); 

connection = DriverManager.getConnection(connectionURL);

if(!connection.isClosed()) {
String newAccountName = null;
String newPassword = null;

newAccountName = request.getParameter("username");
newPassword = generateRandomPassword();

int count_user = 0;
Statement statement0 = null;
ResultSet rs0 = null;
statement0 = connection.createStatement();
String query0 = "SELECT count(*) AS Count FROM Users WHERE AccountName = '" + newAccountName + "';";
rs0 = statement0.executeQuery(query0);
if(rs0.next()) {
count_user = Integer.parseInt(rs0.getString("Count"));

}

if(count_user != 0) {
int count_record = 0;
Statement statement1 = null;
ResultSet rs1 = null;
statement1 = connection.createStatement();
String query1 = "UPDATE Users SET Password = '" + newPassword + "' WHERE AccountName = '" + newAccountName + "';";
count_record = statement1.executeUpdate(query1, Statement.NO_GENERATED_KEYS);
statement1.close();

if(count_record != 0) {
out.print("reset_password_successful");

String host="smtp.gmail.com", user="bookwarm.comp3111h@gmail.com", pass="bookwarmmobile";String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to = newAccountName;
String from = "bookwarm.comp3111h@gmail.com"; 
String subject = "BookwarmMobile - Password Reset Confirmation";
String messageText = "<p>This email is to confirm your recent password reset request. Please find attached your temporary password for accessing our service.</p><p><blockquote>Temporary Password: <b>" + newPassword + "</b></blockquote></p><p>For your security, please change your password after initial signing in and keep your new password safe. You will be asked to enter your user name and password each time you sign on to the site.</p><p>Thank you for using BookwarmMobile.</p>";
boolean sessionDebug = true;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setContent(messageText, "text/html"); 
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);

try {
transport.sendMessage(msg, msg.getAllRecipients());

}
catch (Exception err) {
out.println(err.getMessage());

}

transport.close();

}
else {
out.print("reset_password_failed");

}

}
else {
out.print("no_user_found");

}

statement0.close();

}

connection.close();

}
catch(Exception ex){
out.print(ex);

}
%>